package com.zhudi.chart.view;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Point;
import android.view.Display;
import android.view.View;

import com.zhudi.chart.R;
import com.zhudi.develop.util.ZhudiScreenUtil;

public class ChartView extends View {
	private Context context;
	private int selectI;
	/** 原点的X坐标 */
	public int XPoint;
	/** 原点的Y坐标 */
	public int YPoint;
	/** X的每个刻度长度 */
	public int XScale;
	/** Y的每个刻度长度 */
	public int YScale;
	/** Y的高度 */
	public int height;
	/** X轴的长度 */
	public int XLength;
	public int YLength = 470; // Y轴的长度
	public String[] XLabel; // X的刻度
	public String[] YLabel; // Y的刻度
	public String[] Data; // 数据
	public String Title; // 显示的标题
	private List<PointBean> allpoint = new ArrayList<PointBean>();// 保存所以的操作坐标
	/**x轴文字 */
	private Paint xPaint;
	private Paint yPaint;
	private Paint yTxtPaint;
	public static final int RECT_SIZE = 55;
	private Paint dataPaint;

	private int screenWidth;
	private int screenHeight;

	private String data = "";
	private String dushu = "";
	private float x;
	private float y;
	private Paint ciclePaint;
	/** 用于标记纵坐标的单位 false表示显示w true表示显示kwh */
	private boolean flag;

	public ChartView(Context context) {
		super(context);
		this.context = context;
	}

	/**
	 * 获取数据
	 * 
	 * @param XLabels
	 *            x轴数据
	 * @param YLabels
	 *            y轴数据
	 * @param AllData
	 *            数据内容
	 * @param display
	 *            屏幕对象
	 * @param YPoint
	 * @param height
	 */
	public void SetInfo(String[] XLabels, String[] YLabels, String[] AllData,
			Display display, int YPoint, int height, boolean flag) {
		selectI = 0;
		XPoint = context.getResources().getInteger(R.integer.ZerotoDot);
		XScale = (ZhudiScreenUtil.getScreenWidth(context) - 40) / 8;
		XLabel = XLabels;
		YLabel = YLabels;
		Data = AllData;
		this.YPoint = YPoint;
		XLength = XPoint + (XLabels.length) * XScale + XPoint;
		YLength = height ;
		this.flag = flag;
		YScale = (height - 20) / 4;
		this.height = height;
		this.screenWidth = display.getWidth();
		this.screenHeight = display.getHeight();
		// invalidate();
	}

	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);// 重写onDraw方法
		setDraw(canvas);
	}

	private void setDraw(Canvas canvas) {
		xPaint = new Paint();
		xPaint.setStyle(Paint.Style.STROKE);
		xPaint.setAntiAlias(true);// 去锯齿
		xPaint.setColor(context.getResources().getColor(R.color.black));// 颜色
		xPaint.setTextSize(context.getResources().getInteger(R.integer.xPaintTextSize)); // 设置轴文字大小

		yPaint = new Paint();
		yPaint.setStyle(Paint.Style.STROKE);
		yPaint.setAntiAlias(true);// 去锯齿
		yPaint.setColor(Color.parseColor("#808b98"));// 颜色

		yTxtPaint = new Paint();
		yTxtPaint.setStyle(Paint.Style.STROKE);
		yTxtPaint.setAntiAlias(true);// 去锯齿
		yTxtPaint.setColor(context.getResources().getColor(R.color.white));// 颜色
		yTxtPaint.setTextSize(13); // 设置轴文字大小

		dataPaint = new Paint();
		dataPaint.setStyle(Paint.Style.FILL);
		dataPaint.setAntiAlias(true);// 去锯齿
		dataPaint.setColor(context.getResources().getColor(R.color.blue_char));// 颜色
		dataPaint.setTextSize(22); // 设置轴文字大小
		dataPaint.setStrokeWidth(4);

		ciclePaint = new Paint();
		ciclePaint.setStyle(Paint.Style.FILL_AND_STROKE);
		ciclePaint.setAntiAlias(true);// 去锯齿
		ciclePaint.setColor(Color.parseColor("#37b99c"));// 颜色
		for (int i = 0; i * YScale < YLength; i++) {
			selectI = i;
			canvas.drawLine(XPoint + 20, YPoint - i * YScale, XLength, YPoint - i * YScale, yPaint); // 画横轴
		}

		for (int i = 1; i * XScale < XLength; i++) {
			canvas.drawLine(XPoint + (i - 1) * XScale + 35, YPoint, XPoint
					+ (i - 1) * XScale + 35, YPoint - YLength, yPaint); // 画纵轴
		}
		for (int i = 0; i * XScale < XLength; i++) {
			try {
				// x轴坐标文字
				// if (XLabel[i].equals("00:00")) {
				// xPaint.setColor(context.getResources().getColor(R.color.yellow));//
				// 颜色
				// }
				canvas.drawText(XLabel[i],XPoint + i * XScale + 15,
						YPoint + context.getResources().getInteger(R.integer.XtoTextView), xPaint); // 文字

				PointBean bean = new PointBean();
				Point point = new Point(XPoint + i * XScale + 25, YPoint + 110);
				bean.point = point;
				bean.title = XLabel[i];
				// 数据值
				if (i > 0 && YCoord(Data[i - 1]) != -999 && YCoord(Data[i]) != -999) // 保证有效数据
					// 画线
					canvas.drawLine(XPoint + (i - 1) * XScale + 35,YCoord(Data[i - 1]), XPoint + i * XScale + 35,
							YCoord(Data[i]), dataPaint);
				// 画断端点的小圆
				// if (XLabel[i].equals("00:00")) {
				// dataPaint.setColor(context.getResources().getColor(R.color.yellow));//
				// 颜色
				// }
				canvas.drawCircle(XPoint + i * XScale + 35, YCoord(Data[i]), 8,
						dataPaint);
				bean.dushu = Data[i];
				allpoint.add(bean);
			} catch (Exception e) {
			}
		}
	}

	private float YCoord(String y0) // 计算绘制时的Y坐标，无数据时返回-999
	{
		float y;
		try {
			y = Float.parseFloat(y0);
			// System.out.println("YCoord--->" + y);
		} catch (Exception e) {
			return -999; // 出错则返回-999
		}
		try {
			if (y == 0) {
				return (float) (YPoint);
			} else {
				// System.out.println("距离---》" + (YPoint - (y * YScale)));
				// return (float) (YPoint - (y - 36.2) / 0.2 * YScale);
				// return (float) (YPoint - (y / Float.parseFloat(YLabel[4]) *
				// height));
				if (y == Float.parseFloat(YLabel[4])) {
					// System.out.println("最大");
					// canvas.drawLine(XPoint + 20, YPoint - i * YScale,
					// XLength,
					// YPoint - i * YScale, yPaint); // 画横轴
					return (float) (YPoint - selectI * YScale);
				} else {
					// return (float) (YPoint - (y / Float.parseFloat(YLabel[4])
					// * height));
					return (float) (YPoint - (y / Float.parseFloat(YLabel[4]) * (selectI * YScale)));
					// return (float) (YPoint - (y / Float.parseFloat(YLabel[4])
					// * (YPoint - selectI * YScale)));
				}

			}
			// return (float) (YPoint - (y - 36.2) / 0.2 * YScale);
		} catch (Exception e) {
		}
		return y;
	}

	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		if (XLength > screenWidth) {
			//int with = XLabel.length * XScale + XPoint + 25 * 2;
			//setMeasuredDimension(with, screenHeight);
			setMeasuredDimension(XLength, screenHeight);
		} else {
			setMeasuredDimension(screenWidth, screenHeight);
		}
	}

	@Override
	protected void onScrollChanged(int l, int t, int oldl, int oldt) {
		String tag = "onScrollChanged";
		// Log.e(tag, "Scroll....");
		super.onScrollChanged(l, t, oldl, oldt);

	}

}
